package com.itheima.refect;

import com.itheima.domain.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/*
获取成员变量
给 成员变量赋值
 */
public class Demo03 {

    public static void main(String[] args) throws Exception {
        Class<Student> clazz = (Class<Student>) Class.forName("com.itheima.domain.Student");
       //在给属性赋值之前, 声明给哪一个对象的属性赋值
        Constructor<Student> constractor = clazz.getDeclaredConstructor();
        constractor.setAccessible(true);
        Student student = constractor.newInstance();
        System.out.println("设置前"+student);

        Field school = clazz.getField("school");
        school.set(student,"传智");
        Field name = clazz.getDeclaredField("name");

        System.out.println(school);
        Object o = school.get(student );
        System.out.println("从对象中获取到的学校的值"+o);
        name.setAccessible(true);
        name.set(student,"鹿晗");
        Field age = clazz.getDeclaredField("age");
        age.setAccessible(true);
        age.set(student,28);

        System.out.println("设置后"+student);

    }

}
